#include "ObservedObject.hpp"
#include <iostream>

using namespace std;

template <class T>
ObservedObject<T>::ObservedObject(T p)
{
    _value = p;
}

template <class T>
ObservedObject<T>::~ObservedObject()
{
    for (auto observer : ObserverList)
    {
        delete observer;
    }
}

template <class T>
T ObservedObject<T>::getValue()
{
    return _value;
}

template <class T>
void ObservedObject<T>::setValue(const T p)
{
    _value = p;
    cout << "对象值发生变化(" << _value << ")，提示所有观察者：" << endl;
    notifyAllObserver();
}

template <class T>
void ObservedObject<T>::notifyAllObserver()
{

    for (auto observer : ObserverList)
    {
        observer->notify();
    }
}

template <class T>
void ObservedObject<T>::addObserver(Observer<T> *observer)
{
    ObserverList.push_back(observer);
}